package com.sinosoft.dsp.platform.components.portal.input;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sinosoft.builder.templates.concept.PresentationType;
import com.sinosoft.builder.templates.concept.SearchForm;
import com.sinosoft.builder.templates.concept.SearchType;
import com.sinosoft.builder.templates.concept.SqlConfigType;
import com.sinosoft.builder.templates.concept.TagElementType;
import com.sinosoft.dsp.platform.components.AbstractViewBean;
import com.sinosoft.dsp.platform.components.helper.TagElementHelper;
import com.sinosoft.dsp.platform.exception.SinoPlatformException;
import com.sinosoft.dsp.platform.services.PlatformViewService;
import com.sinosoft.dsp.platform.services.SearchFormService;
import com.sinosoft.dsp.platform.utils.SinoManagedBean;
import com.sinosoft.dsp.system.core.exception.SinoException;
import com.sinosoft.dsp.system.to.IGenericTO;
import com.sinosoft.dsp.system.to.attribute.Attribute;
import com.sinosoft.dsp.system.to.helper.ToHelper;
import com.sinosoft.dsp.system.utils.Const;

/**
 * 查询表单ViewBean
 * 
 * @author 潘巍（PeterPan）
 * @since 2011-6-18 下午03:18:08
 * 
 */
public class SinoSearchFormViewBean extends AbstractViewBean implements
		ISinoSearchFormViewBean {

	public static final String SEARCH_FORM_ID = "searchFormId";

	// 查询表单的元数据
	private SearchForm meta;

	// 查询表单的查询字段
	private Map<String, SinoDefaultSearchConditionBean> conditions = new HashMap<String, SinoDefaultSearchConditionBean>();

	// 查询表单的控制按钮
	private Map<String, SinoDefaultSearchControlBean> controls = new HashMap<String, SinoDefaultSearchControlBean>();

	// 查询的页面数据
	private SinoDefaultSearchBean[] searchs;

	@Override
	public void init(IGenericTO tos) throws SinoException {
		String metaId = null;
		Attribute<?> param = ToHelper.getContentAttriubteByName(tos,
				SEARCH_FORM_ID);
		if (param != null) {
			metaId = param.convertToString();
		}

		if (Const.isEmpty(metaId)) {
			PlatformViewService pvs = SinoManagedBean
					.findBean("platformViewService");
			metaId = pvs.getAttributeValueFromLayout(super.getLayout(),
					SEARCH_FORM_ID);
		}

		if (!Const.isEmpty(metaId)) {
			this.initMeta(metaId);
		}
	}

	@Override
	public void initMeta(String meta) throws SinoPlatformException {
		SearchFormService sfs = SinoManagedBean.findBean("searchFormService");
		this.meta = sfs.getSearchFormById(meta);
		if (this.meta == null) {
			throw new SinoPlatformException("无法获得查询模板描述。");
		}
		initData();
	}

	private void initData() throws SinoPlatformException {
		this.conditions.clear();
		this.controls.clear();
		if (this.meta.getSearch() != null) {
			for (SearchType st : this.meta.getSearch()) {
				if (st.getPresentation() != null) {
					if (st.getPresentation().getSearchCondition() != null) {
						createConditionObject(st.getPresentation()
								.getSearchCondition().getTagElement());
					}

					if (st.getPresentation().getControl() != null) {
						createControlObject(st.getPresentation().getControl()
								.getTagElement());
					}
				}
			}
		}
	}

	private void createControlObject(List<TagElementType> wets) {
		if (wets != null) {
			for (TagElementType tet : wets) {
				createControlObject(tet);
			}
		}
	}

	private void createControlObject(TagElementType type) {
		String id = type.getId();
		SinoDefaultSearchControlBean bean = new SinoDefaultSearchControlBean(
				type);
		this.controls.put(id, bean);
	}

	private void createConditionObject(List<TagElementType> wets)
			throws SinoPlatformException {
		if (wets != null) {
			for (TagElementType tet : wets) {
				createConditionObject(tet);
			}
		}
	}

	private void createConditionObject(TagElementType type)
			throws SinoPlatformException {
		try {
			String id = type.getId();
			if (type.getAttributeType() != null) {
				if (this.conditions.containsKey(id)) {
					throw new SinoPlatformException("标签参数ID出现重复错误（" + id + "）！");
				}
				SinoDefaultSearchConditionBean bean = new SinoDefaultSearchConditionBean(
						type);

				bean.setValue(TagElementHelper.createNewTagValue(type));
				this.conditions.put(id, bean);
			}
		} catch (Exception e) {
			throw new SinoPlatformException("创建条件对象出现错误。");
		}
	}

	@Override
	public SearchForm getMeta() {
		return meta;
	}

	@Override
	public SinoDefaultSearchBean[] getSearchs() throws SinoPlatformException {
		if (searchs == null) {
			List<SearchType> searchTypes = this.meta.getSearch();
			searchs = new SinoDefaultSearchBean[searchTypes.size()];
			for (int i = 0; i < searchTypes.size(); i++) {
				SinoDefaultSearchBean searchBean = new SinoDefaultSearchBean();

				// 设置查询元数据
				PresentationType searchMeta = searchTypes.get(i)
						.getPresentation();
				searchBean.setMeta(searchMeta);

				// 初始化数据列表ViewBean
				SqlConfigType sqlConfig = searchTypes.get(i).getSqlConfig();
				searchBean.initDatas(sqlConfig);

				// 设置Form对象this
				searchBean.setForm(this);

				searchs[i] = searchBean;
			}
		}

		return searchs;
	}

	/**
	 * 刷新查询条件
	 * 
	 * @param id
	 *            查询页面ID
	 */
	@Override
	public void flushSearchs(String id) {
		if (!Const.isEmpty(id)) {
			for (SinoDefaultSearchBean s : searchs) {
				if (id.equals(s.getMeta().getId())) {
					s.flushSearchs();
					break;
				}
			}
		}
	}

	/**
	 * 刷新按钮
	 * 
	 * @param id
	 */
	@Override
	public void flushButtons(String id) {
		if (!Const.isEmpty(id)) {
			for (SinoDefaultSearchBean s : searchs) {
				if (id.equals(s.getMeta().getId())) {
					s.flushButtons();
					break;
				}
			}
		}
	}

	/**
	 * 改变查询条件的显示状态
	 * 
	 * @param id
	 * @param display
	 */
	@Override
	public void changeTagDisplayState(String id, boolean display) {
		SinoDefaultSearchConditionBean condition = this.conditions.get(id);
		if (condition != null) {
			condition.setDisplay(display);
		}

	}

	public SinoDefaultSearchConditionBean getCondition(String id) {
		return this.conditions.get(id);
	}

	public SinoDefaultSearchControlBean getControl(String id) {
		return this.controls.get(id);
	}

}
